<template>
  <div class="goods">
    <div class="wrap">
      <Crumb> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            v-for="(item, index) in option1"
            :key="index"
            @click="option1Click(index, item.min, item.max, item.type)"
            :class="flag1 == index ? 'active' : ''"
            >{{ item.text }}
          </span>
        </li>
        <li>
          <strong>分类：</strong>
          <span
            v-for="(item, index) in option2"
            :key="index"
            @click="option2Click(index)"
            :class="flag2 == index ? 'active' : ''"
          >
            {{ item.text }}
          </span>
        </li>
      </ul>
      <List :arr="goodsListShow" :num="100" />
      <p class="loading"><i class="iconfont icon-loading" v-show="!isNodata"></i>{{isNodata?"已经到底了":"正在加载"}}</p>
    </div>
  </div>
</template>                            
<script>
import { GitListAPI } from "@/request/api";
import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import { getClientHeight, getScrollTop, getScrollHeight } from "../utils";
export default {
  data() {
    return {
      did: 0,
      type: 0,
      min: 0,
      max: 10000,
      keyword: "",
      types: 0,
      option1: [
        { min: 0, max: 100000, text: "全部", type: 1 },
        { min: 0, max: 0, text: "我能兑换的", type: 2 },
        { min: 0, max: 500, text: "0-500个", type: 3 },
        { min: 500, max: 1000, text: "500-1000个", type: 4 },
        { min: 1000, max: 1500, text: "1000-1500个", type: 5 },
        { min: 1500, max: 2500, text: "1500-2500个", type: 6 },
        { min: 2500, max: 6500, text: "2500-6500个", type: 7 },
        { min: 6500, max: 10000, text: "6500-10000个", type: 8 },
      ],
      // option1: [
      //   { min: 0, max: 100000, text: "全部" },
      //   { min: 0, max: 0, text: "我能兑换的" },
      //   { min: 0, max: 500, text: "0-500个" },
      //   { min: 500, max: 1000, text: "500-1000个" },
      //   { min: 1000, max: 1500, text: "1000-1500个" },
      //   { min: 1500, max: 2500, text: "1500-2500个" },
      //   { min: 2500, max: 6500, text: "2500-6500个" },
      //   { min: 6500, max: 10000, text: "6500-10000个" },
      // ],
      // 实物礼品 虚拟礼品
      option2: [
        { type: 0, text: "全部" },
        { type: 1, text: "实物礼品" },
        { type: 2, text: "虚拟礼品" },
      ],
      flag1: 0,
      flag2: 0,
      goodsList: [],
      goodsListShow: [],
      page: 1,
      size: 8,
      isLoading: false, //节流
      isNodata:false,//是否没有数据
    };
  },
  components: {
    Crumb,
    List,
  },

  created() {
    this.keyword = this.$route.query.keyword;
    this.getListInfo();
  },
  mounted() {
    //添加滚动事件的监听
    window.addEventListener("scroll", this.scrollFn);
  },
  beforeDestroy() {
    //删除全局事件
    window.removeEventListener("scroll", this.scrollFn);
  },
  methods: {
    option1Click(index, min, max, type) {
      //

      if (this.types == type) {
        return;
      }
      this.types = type;
      this.min = min;
      this.max = max;
      this.flag1 = index;
      this.getListInfo();
    },
    option2Click(index) {
      this.flag2 = index;
    },
    async getListInfo() {
      let res = await GitListAPI({
        did: this.did,
        type: this.type,
        min: this.min,
        max: this.max,
        keyword: this.keyword,
      });
      console.log("GitListAPI", res.data);
      this.goodsList = res.data;
      this.goodsListShow = this.goodsList.filter((item, index) => index < 8);

      //初始化数据
      this.page = 1;
      this.isLoading = false;
      this.isNodata = false;
      //判读是不是没有数据了
       if(this.goodsListShow.length>=this.goodsList.length){
          this.isNodata = true
          
        }
    },
    scrollFn() {
      //频繁触发事件
      //需求，来到底部才来加载
      //判断，是不是到底部了
      //if(窗口高度 + 超出窗口高度 === 总页面高度)
      if (getClientHeight() + getScrollTop() >= getScrollHeight() - 20) {
        //在加载之前还有数据吗
        if(this.goodsListShow.length>=this.goodsList.length){
          this.isNodata = true
          return
        }
        //加载的时候必须保证isLoading为false，如果为true就不能进行加载
        if (!this.isLoading) {
          this.isLoading = true;
          this.page++;
          for (
            let i = this.size * (this.page - 1);
            i < this.size * this.page;
            i++
          ) {
            this.goodsList[i] ? this.goodsListShow.push(this.goodsList[i]) : "";
          }
          this.isLoading = false;
        }
      }
    },
  },
  watch: {
    "$route.query.keyword": {
      handler(newVal, oldVal) {
        //什么时候执行？？地址栏上的keyword一变化就执行
        this.keyword = newVal;
        this.getListInfo();
      },
    },
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/style/global.less";

.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @basecolor;
          font-weight: bold;
        }
      }
    }
  }
}
.loading {
  text-align: center;
  padding: 10px 0;
}
</style>